package com.ruoyi.relation.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.relation.mapper.DistributionRelationMapper;
import com.ruoyi.relation.domain.DistributionRelation;
import com.ruoyi.relation.service.IDistributionRelationService;

/**
 * 分销关系信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class DistributionRelationServiceImpl implements IDistributionRelationService 
{
    @Autowired
    private DistributionRelationMapper distributionRelationMapper;

    /**
     * 查询分销关系信息列表
     *
     * @param distributorId 分销商id
     * @return 分销关系信息列表
     */
    @Override
    public List<DistributionRelation> selectSubordinatesByDistributorId(Long distributorId) {
        return distributionRelationMapper.selectSubordinatesByDistributorId(distributorId);
    }

    /**
     * 查询分销关系信息
     * 
     * @param relationId 分销关系信息主键
     * @return 分销关系信息
     */
    @Override
    public DistributionRelation selectDistributionRelationByRelationId(Long relationId)
    {
        return distributionRelationMapper.selectDistributionRelationByRelationId(relationId);
    }

    /**
     * 查询分销关系信息列表
     * 
     * @param distributionRelation 分销关系信息
     * @return 分销关系信息
     */
    @Override
    public List<DistributionRelation> selectDistributionRelationList(DistributionRelation distributionRelation)
    {
        return distributionRelationMapper.selectDistributionRelationList(distributionRelation);
    }

    /**
     * 新增分销关系信息
     * 
     * @param distributionRelation 分销关系信息
     * @return 结果
     */
    @Override
    public int insertDistributionRelation(DistributionRelation distributionRelation)
    {
        distributionRelation.setCreateTime(DateUtils.getNowDate());
        return distributionRelationMapper.insertDistributionRelation(distributionRelation);
    }

    /**
     * 修改分销关系信息
     * 
     * @param distributionRelation 分销关系信息
     * @return 结果
     */
    @Override
    public int updateDistributionRelation(DistributionRelation distributionRelation)
    {
        distributionRelation.setUpdateTime(DateUtils.getNowDate());
        return distributionRelationMapper.updateDistributionRelation(distributionRelation);
    }

    /**
     * 批量删除分销关系信息
     * 
     * @param relationIds 需要删除的分销关系信息主键
     * @return 结果
     */
    @Override
    public int deleteDistributionRelationByRelationIds(Long[] relationIds)
    {
        return distributionRelationMapper.deleteDistributionRelationByRelationIds(relationIds);
    }

    /**
     * 根据分销商id和下级id查询分销关系信息
     *
     * @param distributorId 分销商id
     * @param subordinateId 下级id
     * @return 分销关系信息
     */
    @Override
    public DistributionRelation selectRelationByDistributorIdAndSubordinateId(Long distributorId, Long subordinateId)
    {
        // 创建查询条件
        DistributionRelation relation = new DistributionRelation();
        // 设置分销商id
        relation.setDistributorId(distributorId);
        relation.setSubordinateId(subordinateId);
        List<DistributionRelation> list = distributionRelationMapper.selectDistributionRelationList(relation);
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * 根据下级分销员ID删除所有相关的关系记录
     *
     * @param subordinateId 下级分销员ID
     * @return 结果
     */
    @Override
    public int deleteRelationBySubordinateId(Long subordinateId)
    {
        return distributionRelationMapper.deleteRelationBySubordinateId(subordinateId);
    }

    /**
     * 删除分销关系信息信息
     * 
     * @param relationId 分销关系信息主键
     * @return 结果
     */
    @Override
    public int deleteDistributionRelationByRelationId(Long relationId)
    {
        return distributionRelationMapper.deleteDistributionRelationByRelationId(relationId);
    }
}
